package fpt.store.main;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ConfigurationInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Window;
import android.widget.Toast;

public class SplashScreen extends Activity {

	private static final int TIME = 4 * 1000;
	private Context context;
	private boolean flags = false;
	private SharedPreferences appPreferences;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splashscreen);
		shortcutIcon();
		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
			}
		}, TIME);
		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				boolean flags = isNetworkConnected();
				// in activity onCreate
				AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
						SplashScreen.this);
				if (!detectOpenGLES20()) {
					alertDialogBuilder.setTitle("Xin lỗi!!!");
					alertDialogBuilder
							.setMessage(
									"Dòng máy bạn đang sử dựng không thể truy cập Google Map phiên bản mới.")
							.setPositiveButton("Thoát",
									new DialogInterface.OnClickListener() {

										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
											finish();
										}
									});
					AlertDialog alertDialog = alertDialogBuilder.create();
					alertDialog.show();
				} else if (flags == false) {
					// set title
					alertDialogBuilder.setTitle("Internet chưa được bật!");
					alertDialogBuilder
							.setMessage(
									"Bạn có muốn tiếp tục truy cập ứng dụng hay không?")
							.setCancelable(false)
							.setPositiveButton("Có",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog, int id) {
											Intent intent = new Intent(
													SplashScreen.this,
													StoreMain.class);
											startActivity(intent);
											SplashScreen.this.finish();
											overridePendingTransition(
													R.anim.fade_in,
													R.anim.fade_out);
										}
									})
							.setNegativeButton("Không",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog, int id) {
											SplashScreen.this.finish();
										}
									});
					AlertDialog alertDialog = alertDialogBuilder.create();
					alertDialog.show();
				} else {
					Intent intent = new Intent(SplashScreen.this,
							StoreMain.class);
					startActivity(intent);
					SplashScreen.this.finish();
					overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
				}
			}
		}, TIME);
	}

	@Override
	public void onBackPressed() {
		this.finish();
		super.onBackPressed();
	}

	private boolean isNetworkConnected() {
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo ni = cm.getActiveNetworkInfo();
		if (ni == null) {
			// There are no active networks.
			return false;
		} else
			return true;
	}

	private boolean detectOpenGLES20() {
		ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
		ConfigurationInfo info = am.getDeviceConfigurationInfo();
		return (info.reqGlEsVersion >= 0x20000);
	}

	private void shortcutIcon() {
		appPreferences = PreferenceManager
				.getDefaultSharedPreferences(getApplicationContext());
		flags = appPreferences.getBoolean("isAppInstalled", false);
		if (flags == false) {
			Intent shortcutIntent = new Intent(getApplicationContext(),
					SplashScreen.class);
			shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

			Intent addIntent = new Intent();
			addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
			addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Cửa hàng FPT Shop");
			addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
					Intent.ShortcutIconResource.fromContext(
							getApplicationContext(), R.drawable.ic_launcher));
			addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
			getApplicationContext().sendBroadcast(addIntent);
			Toast.makeText(getApplicationContext(),
					"Phím tắt Cửa hàng FPT Shop đã cài đặt thành công",
					Toast.LENGTH_LONG);
			SharedPreferences.Editor editor = appPreferences.edit();
			editor.putBoolean("isAppInstalled", true);
			editor.commit();
		}
	}

}
